<?php

    require_once('mediainfo.php');

    class importVideos extends mediainfo{

        var $vconfig = array();

        function importVideos(){
            $this->vconfig = parse_ini_file('video/vconfig.ini', true);
            echo "<pre>";
            print_r($this->vconfig);
        }

        /*
         * Importer für Videos
         * Convertiert alle Frontendvideos und Backendvideos
         * Pre Eintrag Datenbank
         */
        function importVideo($fullPathToVideo){

            $mediaInfo = $this->getMediaInfo($fullPathToVideo);

            $videoFormat = $this->checkVideoFormat($mediaInfo);
            echo "<pre>";
            print_r($videoFormat);
            echo "<pre>";
            print_r($mediaInfo);

            //Prüfen welches Format das zu Importierende Video hat


            //Test
            $optionArray['vcodec']      = "mp4";
            $optionArray['level']       = 12;
            $optionArray['bitrate']     = $this->vconfig['bitrate']['vcd'];
            $optionArray['size']        = $this->vconfig['frontend']['xy_small'];
            $optionArray['tragetdir']   = $this->vconfig['frontend']['xy_small_path'];
            $this->exportVideo($fullPathToVideo, $optionArray);

            $optionArray['vcodec']      = "mp4";
            $optionArray['level']       = 12;
            $optionArray['bitrate']     = $this->vconfig['bitrate']['hdsmall'];
            $optionArray['size']        = $this->vconfig['frontend']['xy_medium'];
            $optionArray['tragetdir']   = $this->vconfig['frontend']['xy_medium_path'];
            $this->exportVideo($fullPathToVideo, $optionArray);



        }

        /*
         * Aufruf für den Export der Videos
         */
        function exportVideo($fullPathToVideo, $optionArray = array()){

            $exec = "ffmpeg -i ".$fullPathToVideo. " ".         //Orginal Video
                    "-vcodec libx264 ".                         //Videocodec
                    "-level ".$optionArray['level'] . " ".      //Quaili Level
                    "-b ".$optionArray['bitrate'] . " ".        //Bitrate
                    "-r 25 ".                                   //Framerate
                    "-bt ".$optionArray['bitrate'] . " ".       //Set video bitrate tolerance (in bits, default 4000k). Has a minimum value of: (target_bitrate/target_framerate). In 1-pass mode, bitrate tolerance specifies how far ratecontrol is willing to deviate from the target average bitrate value. This is not related to min/max bitrate. Lowering tolerance too much has an adverse effect on quality
                    "-s ".$optionArray['size']. " ".            //Videogröße
                    "-coder 1 ".
                    "-flags +loop ".
                    "-cmp +chroma ".
                    "-partitions +parti8x8+parti4x4+partp8x8+partb8x8 ".
                    "-me_method full ".                         //Set motion estimation method to method. Available methods are (from lowest to best quality):
                    "-subq 7 ".
                    "-me_range 23 ".
                    "-g 250 ".                                  //Set the group of pictures size.
                    "-keyint_min 25 ".
                    "-sc_threshold 40 ".
                    "-i_qfactor 0.71 ".
                    "-b_strategy 1 ".
                    "-qcomp 0.6 ".
                    "-qmin 10 ".
                    "-qmax 51. ".
                    "-qdiff 4. ".                                //maximum difference between the quantizer scales (VBR)
                    "-bf 16 ".                                   //Use ’frames’ B-frames (supported for MPEG-1, MPEG-2 and MPEG-4).
                    "-refs 6 ".
                    "-directpred 2 ".
                    "-trellis 1 ".
                    "-flags2 +bpyramid+wpred+mixed_refs+dct8x8+fastpskip ".
                    "-f ".$optionArray['vcodec']." ".                                  //format
                    "-acodec libfaac ".
                    "-ab 56000 ".
                    "-ac 2 ".
                    "-ar 48000 ".                          //Audio Bitrate
                    $optionArray['tragetdir']."/serenity.mp4";
            echo $exec;
            exec($exec);
        }



        function checkVideoFormat($mediaInfo){

            if($mediaInfo['video']['Width'] < 1280 ||  $mediaInfo['video']['Height'] < 720){

                return "no hd";

            }elseif($mediaInfo['video']['Width'] == 1280 ||  $mediaInfo['video']['Height'] == 720){

                return "hd ready";

            }elseif($mediaInfo['video']['Width'] == 1280 ||  $mediaInfo['video']['Height'] < 720){

                return "hd ready, must be padded";

            }elseif($mediaInfo['video']['Width'] == 1920 ||  $mediaInfo['video']['Height'] == 1080){

                return "hd full";

            }elseif($mediaInfo['video']['Width'] == 1920 ||  $mediaInfo['video']['Height'] < 1080){

                return "hd ready, must be padded";

            }else{

                return "video ungeeignet";

            }

        }

    }


    $newImport = new importVideos();
    $newImport->importVideo('/home/www/workspaces/mklein/oncut/core/serenity.mp4');
?>

